package com.sgb.kjwl.view.ui.fragment.pick;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentPickCompanyDetailBinding;
import com.sgb.kjwl.di.pick.DaggerPickProjectComponent;
import com.sgb.kjwl.di.pick.PickProjectModule;
import com.sgb.zhaocai.view.demand.fragment.WebDemandForecastFragment;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.user.PickCompanyEntity;
import com.sgb.kjwl.view.ui.activity.material.hall.NewPickBiddingListFragment;
import com.sgb.kjwl.view.ui.activity.material.hall.PickNoticeFragment;
import com.sgb.kjwl.viewmodel.pick.PickCompanyDetailFragmentModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * 招采企业
 */
public class PickCompanyDetailFragment extends AppLazyFragment {

    private FragmentPickCompanyDetailBinding binding;

    @Inject
    PickCompanyDetailFragmentModel viewModel;
    private SlidingFragmentPagerAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    String[] titles = new String[5];
    private String comNo;
    int position = 0;

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerPickProjectComponent.builder().appComponent(appComponent).pickProjectModule(new PickProjectModule(this)).build().inject(this);
    }

    public static PickCompanyDetailFragment newInstance(String comNo) {
        PickCompanyDetailFragment fragment = new PickCompanyDetailFragment();
        Bundle bundle = new Bundle();
        bundle.putString("comNo", comNo);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_pick_company_detail, container, false);
        return binding.getRoot();
    }

    @Override
    protected void initView() {
        Bundle bundle =this.getArguments();//得到从Activity传来的数据
        if (bundle != null) {
            comNo = bundle.getString("comNo");
        }
    }

    @Override
    protected void onLazyLoad() {
        viewModel.getCountOfCompanyStoreInfo(comNo);
    }

    public void showCount(List<PickSelectEntity> pickSelectEntityList) {
        if (pickSelectEntityList==null&&pickSelectEntityList.size()==0){
            return;
        }
        for (PickSelectEntity pickSelectEntity : pickSelectEntityList) {
            if (pickSelectEntity.getPickerViewText().equals("全部")) {
                titles[0] = "全部(" + (Integer.valueOf(pickSelectEntity.getNo()) > 999 ? "999+" : pickSelectEntity.getNo()) + ")";
            } else if (pickSelectEntity.getPickerViewText().equals("招标公告")) {
                titles[1] = "招标公告(" + (Integer.valueOf(pickSelectEntity.getNo()) > 999 ? "999+" : pickSelectEntity.getNo()) + ")";
            } else if (pickSelectEntity.getPickerViewText().equals("直接竞价")) {
                titles[2] = "直接竞价(" + (Integer.valueOf(pickSelectEntity.getNo()) > 999 ? "999+" : pickSelectEntity.getNo()) + ")";
            } else if(pickSelectEntity.getPickerViewText().equals("需求预告")){
                titles[3] = "需求预告(" + (Integer.valueOf(pickSelectEntity.getNo()) > 999 ? "999+" : pickSelectEntity.getNo()) + ")";
            }else if(pickSelectEntity.getPickerViewText().equals("招标预告")){
                titles[4] = "招标预告(" + (Integer.valueOf(pickSelectEntity.getNo()) > 999 ? "999+" : pickSelectEntity.getNo()) + ")";
            }
        }
        viewModel.getCompanyData(comNo);
    }

    public void showCompanyData(PickCompanyEntity pickCompanyEntity) {
        try {
            mFragments = new ArrayList<>();
            for (int i = 0; i < titles.length; i++) {
                if (i == 0){
                    mFragments.add(PickHallAllFragment.newInstance(0,"company",comNo));
                }else if (i == 1){
                    mFragments.add(PickHallBidingFragment.newInstance("3",comNo));
                }else if (i == 2){
                    mFragments.add(NewPickBiddingListFragment.newInstance("4",comNo));
                }else if (i == 3){
                    mFragments.add(WebDemandForecastFragment.newInstance(comNo));
                }else if (i == 4){
                    mFragments.add(PickNoticeFragment.newInstance("4",comNo));
                }
            }
            pagerAdapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), mFragments, titles);
            binding.viewpager.setAdapter(pagerAdapter);
            binding.viewpager.setCurrentItem(position);
            binding.viewpager.setOffscreenPageLimit(mFragments.size());
            binding.tabLayout.setupWithViewPager(binding.viewpager);
            binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int i, float v, int i1) {
                }

                @Override
                public void onPageSelected(int i) {
                    position = i;
                    if (position==3){
                        binding.viewLine.setVisibility(View.GONE);
                    }else {
                        binding.viewLine.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onPageScrollStateChanged(int i) {
                }
            });
        } catch (Exception e) {
            MLog.e("招采企业",e.toString());
        }
    }

    public void scrollToTop(){
        int position = binding.viewpager.getCurrentItem();
        if (position == 0) {
            PickPickingListFragment pickPickingListFragment = (PickPickingListFragment) mFragments.get(0);
            pickPickingListFragment.fullScroll();
        } else if (position == 1) {
            PickBidListFragment pickBidListFragment = (PickBidListFragment) mFragments.get(1);
            pickBidListFragment.fullScroll();
        } else if (position == 2) {
            PickProjectListFragment pickProjectListFragment = (PickProjectListFragment) mFragments.get(2);
            pickProjectListFragment.fullScroll();
        }
    }
}
